package com.glory.dmegcpda.adapter;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.glory.dmegcpda.R;
import com.glory.dmegcpda.utils.BitmapUtils;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

import java.io.File;
import java.util.ArrayList;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;


public class ReleaseMsgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Activity context;
    private ArrayList<String> mList;
    private FragmentManager fragmentManager;
    private final LayoutInflater inflater;
    private static final int ITEM_TYPE_ONE = 0x00001;
    private static final int ITEM_TYPE_TWO = 0x00002;
    private Uri imageUri;
    /**
     *这里之所以用多行视图，因为我们默认的有一张图片的（那个带+的图片，用户点击它才会才会让你去选择图片）
     *集合url为空的时候，默认显示它，当它达到集合9时，这个图片会自动隐藏。
     */
    public ReleaseMsgAdapter(Activity context, FragmentManager fragmentManager,ArrayList<String> mList) {
        this.context = context;
        this.mList = mList;
        this.imageUri = imageUri;
        this.fragmentManager = fragmentManager;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        parent.setPadding(20, 0, 20, 0);
        switch (viewType) {
            case ITEM_TYPE_ONE:
                return new MyHolder(inflater.inflate(R.layout.activity_message_item, parent, false));
            case ITEM_TYPE_TWO:
                return new MyTWOHolder(inflater.inflate(R.layout.activity_message_two_item, parent, false));
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyHolder) {
            bindItemMyHolder((MyHolder) holder, position);
        } else if (holder instanceof MyTWOHolder) {
            bindItemTWOMyHolder((MyTWOHolder) holder, position);
        }
    }

    private void bindItemTWOMyHolder(final MyTWOHolder holder, int position) {
        if (listSize() >= 9) {//集合长度大于等于9张时，隐藏 图片
            holder.imageview2.setVisibility(View.GONE);
        }
        holder.imageview2.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View v) {

                FreeApi_DialogUtils.getConfirmAndCancelDialog(context,"拍照","相册","请选择拍照还是相册选取", new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        BitmapUtils.takePicture(context,1);
                    }
                }, () -> {
                    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
                        if (ContextCompat.checkSelfPermission(context,Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
                            //没有权限则申请权限
                            ActivityCompat.requestPermissions(context,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
                        }else {
                            //有权限直接执行,docode()不用做处理
//                            RZAlbum.ofAppName("RZAlbum")
//                                    .setDialogIcon(R.drawable.img_actionbar_back)
//                                    .setToolBarTitle("选取照片")
//                                    .start(context, PHOTO_IMAGE);

                        }
                    }else {
                        //小于6.0，不用申请权限，直接执行
//                        RZAlbum.ofAppName("RZAlbum")
//                                .setDialogIcon(R.drawable.img_actionbar_back)
//                                .setToolBarTitle("选取照片")
//                                .start(context, PHOTO_IMAGE);
                    }
                }).show(fragmentManager, "");
            }
        });
    }



    private void bindItemMyHolder(MyHolder holder, int position) {
        holder.imageview.setImageURI(Uri.fromFile(new File(mList.get(position))));
//        Bitmap bmpDefaultPic = BitmapFactory.decodeFile(mList.get(position));
//        Bitmap bitmap = rotateBitmapByDegree(bmpDefaultPic, getBitmapDegree(mList.get(position)));
//        holder.imageview.setImageBitmap(zoomImg(bitmap, 120, 120));

    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return ITEM_TYPE_TWO;
        } else {
            return ITEM_TYPE_ONE;
        }
    }

    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private final ImageView imageview;
        public MyHolder(View itemView) {
            super(itemView);
            imageview = (ImageView) itemView.findViewById(R.id.imageview);
        }
    }

    class MyTWOHolder extends RecyclerView.ViewHolder {
        private final ImageView imageview2;
        public MyTWOHolder(View itemView) {
            super(itemView);
            imageview2 = (ImageView) itemView.findViewById(R.id.imageview2);
        }
    }
    //对外暴露方法  。点击添加图片（类似于上啦加载数据）
    public void addMoreItem(ArrayList<String> loarMoreDatas) {
        mList.addAll(loarMoreDatas);
        notifyDataSetChanged();
    }
    //得到集合长度
    public int listSize() {
        return mList.size();
    }
    /**
     * 壓縮Bitmap
     */
    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        //获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        //计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        //取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
       /* if(width>height){
            matrix.postRotate(180);

        }*/
        //得到新的图片
        return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
    }
}
